#include "graphics/utils.h"
#include "volume_renderer.h"

namespace venus {
VolumeRenderer::VolumeRenderer(CGcontext cg_context, VolumeBuffer *volume)
    : cg_context_(cg_context),
      volume_(volume),
      density_(0.01),
      brightness_(2.0) {
  LoadPrograms();
}
VolumeRenderer::~VolumeRenderer() {
  delete raymarch_vprog_;
  delete raymarch_fprog_;
}
void VolumeRenderer::LoadPrograms() {
  raymarch_vprog_ = new ProgramCG(cg_context_, 
                                   "shaders/raymarch.cg", 
                                   "RayMarchVP", CG_GL_VERTEX);
  raymarch_fprog_ = new ProgramCG(cg_context_,
                                   "shaders/raymarch.cg",
                                   "RayMarchFP", CG_GL_FRAGMENT);
  density_param_ = raymarch_fprog_->GetParameter("density");
  brightness_param_ = raymarch_fprog_->GetParameter("brightness");
}
// render using ray marching
void VolumeRenderer::Render() {
  raymarch_vprog_->Activate();
  raymarch_fprog_->Activate();
  cgGLSetParameter1f(density_param_, density_);
  cgGLSetParameter1f(brightness_param_, brightness_);

  glActiveTextureARB(GL_TEXTURE0_ARB);
  glBindTexture(GL_TEXTURE_3D, volume_->texture());

  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  glutSolidCube(2.0);

  raymarch_vprog_->Deactivate();
  raymarch_fprog_->Deactivate();
}
} // namespace venus 
